package com.ldk.framework.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alipay.util.AlipayCore;
import com.ldk.base.framework.common.CustomPageable;
import com.ldk.base.framework.util.ConvertUtil;
import com.ldk.base.framework.util.DateUtil;
import com.ldk.base.framework.util.SystemUtil;
import com.ldk.framework.entity.Alipay;
import com.ldk.framework.entity.Driver;
import com.ldk.framework.entity.DriverReceiveTrip;
import com.ldk.framework.entity.OrderCancel;
import com.ldk.framework.entity.OrderCreate;
import com.ldk.framework.entity.OrderMatch;
import com.ldk.framework.entity.PlatformFundFlowRecord;
import com.ldk.framework.entity.PushMessage;
import com.ldk.framework.entity.RechargeRecord;
import com.ldk.framework.entity.ServicePriceRecord;
import com.ldk.framework.entity.ServiceTrip;
import com.ldk.framework.entity.ServiceTripCrews;
import com.ldk.framework.entity.Tenpay;
import com.ldk.framework.entity.TripRefundRecord;
import com.ldk.framework.entity.TripStatusRecord;
import com.ldk.framework.entity.User;
import com.ldk.framework.enums.FlowInOut;
import com.ldk.framework.enums.FlowPurpose;
import com.ldk.framework.enums.ServiceCode;
import com.ldk.framework.enums.TripCrewStatus;
import com.ldk.framework.enums.TripNoType;
import com.ldk.framework.enums.TripRefundStatus;
import com.ldk.framework.enums.PayType;
import com.ldk.framework.enums.PlatformUserType;
import com.ldk.framework.enums.RechargeFrom;
import com.ldk.framework.enums.RechargeType;
import com.ldk.framework.enums.TradePayStatus;
import com.ldk.framework.enums.TripStatus;
import com.ldk.framework.push.CustomMsgModule;
import com.ldk.framework.push.CustomMsgType;
import com.ldk.framework.push.PushCustomMsg;
import com.ldk.framework.push.PushTargetApp;
import com.ldk.framework.service.IZXPCServiceTripService;
import com.weixin.api.WxMpService;
import com.weixin.api.WxMpServiceImpl;
import com.weixin.bean.RefundResData;

/**
 * 专线拼车业务
 *
 * @author xiangwl
 * 2017年3月9日
 * com.ldk.framework.service.impl
 * ZXPCServiceTripServic
 */
@Service
public class ZXPCServiceTripService extends ServiceTripService implements
        IZXPCServiceTripService {

    @Override
    @Transactional
    public Boolean publish(final String dbKey, final ServiceTrip entity) throws Exception {
        if (entity == null)
            return false;

        Boolean success = false;
        int row = insert(dbKey, entity);
        if (row > 0) {
            //添加状态记录
            TripStatusRecord record = new TripStatusRecord();
            record.setOrderNo(entity.getTripNo());
            record.setStatusName(TripStatus.Publish.getName());
            record.setStatusValue(TripStatus.Publish.getValue());
            record.setTip(TripStatus.Publish.getTip());
            record.setTime(SystemUtil.getAllSeconds());
            row = tripStatusRecordService.insert(dbKey, record);

            String distance = 0 + "m";
            if (entity.getDistance() != null && entity.getDistance() > 0) {
                if (entity.getDistance() > 1000) {
                    distance = ConvertUtil.toRoundFiexd(entity.getDistance() / 1000) + "公里";
                } else {
                    distance = entity.getDistance() + "米";
                }
            } else {
            }

            //插入费用明细
            ServicePriceRecord pricerecord = new ServicePriceRecord();
            pricerecord.setPrice(entity.getPayFee());
            pricerecord.setTripNo(entity.getTripNo());
            pricerecord.setName("里程（" + distance + "）");
            pricerecord.setDes("里程" + distance);
            pricerecord.setUpdateTime(SystemUtil.getAllSeconds());
            servicePriceRecordService.insert(dbKey, pricerecord);

            //推送给司机进行接单
            pushTripToDriver("", entity);

            //插入订单发起表
            try {
                OrderCreate orderCreate = new OrderCreate();
                orderCreate.setCompanyId("5201HDCX8779");
                orderCreate.setAddress(520100L);
                orderCreate.setOrderId(entity.getTripNo());
                Date start = new Date(entity.getCreateTime() * 1000L);
                orderCreate.setDepartTime(Long.parseLong(DateUtil.DatetoString(start, "yyyyMMddHHmmss")));
                orderCreate.setOrderTime(Long.parseLong(DateUtil.DatetoString(start, "yyyyMMddHHmmss")));
                orderCreate.setPassengerNote(entity.getRemark());
                orderCreate.setDeparture(entity.getStartLocation());
                orderCreate.setDepLongitude(entity.getStartLon());
                orderCreate.setDepLatitude(entity.getStartLat());
                orderCreate.setDestination(entity.getEndLocation());
                orderCreate.setDestLongitude(entity.getEndLon());
                orderCreate.setDestLatitude(entity.getEndLat());
                orderCreate.setEncrypt(3L);
                orderCreate.setFareType("HDCX002");
                orderCreate.setOrderTime2(0L);
                orderCreate.setDestAddress("520100");
                User user = userService.getUserByOpenId("", entity.getOpenId());
                if(user!=null){
                    orderCreate.setPassengerName(SystemUtil.desensitizedName(user.getNickName()));
                    orderCreate.setPassengerPhone(SystemUtil.hidePhoneNo(user.getPhoneNo()));
                   // orderCreate.setPassengerGender("");
                }
                orderCreateService.insertUpper("", orderCreate);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //如果定金小于等于0直接就到支付定金状态
		/*if(entity.getDepositPayFee() != null && entity.getDepositPayFee() <= 0){
			ServiceTrip newEntity = new ServiceTrip();
			newEntity.setTripNo(entity.getTripNo());
			newEntity.setDepositPayId(PayType.Balance.getId());
			newEntity.setDepositPayName(PayType.Balance.getName());
			newEntity.setDepositPayStatus(TradePayStatus.TRADE_SUCCESS.getValue());
			newEntity.setDepositPayTime(SystemUtil.getAllSeconds());
			newEntity.setStatus(TripStatus.DepositPayed.getValue());

			int row1 = updateByKey(dbKey, newEntity);
			if(row1 > 0){
				TripStatusRecord record = new TripStatusRecord();
				record.setOrderNo(entity.getTripNo());
				record.setStatusName(TripStatus.DepositPayed.getName());
				record.setStatusValue(TripStatus.DepositPayed.getValue());
				record.setTip(TripStatus.DepositPayed.getTip());
				record.setTime(SystemUtil.getAllSeconds());

				row = tripStatusRecordService.insert(dbKey, record);
			}
		}*/
        success = row > 0;

        return success;
    }

    @Override
    @Transactional
    public Boolean userCancel(final String dbKey, final ServiceTrip userTrip, User user, Boolean freeCancel) throws Exception {
        if (userTrip == null || StringUtils.isBlank(userTrip.getTripNo()))
            return false;

        final String tripNo = userTrip.getTripNo();
        final String openId = userTrip.getOpenId();

        ServiceTrip entity = new ServiceTrip();
        entity.setTripNo(tripNo);
        entity.setStatus(TripStatus.UserCancel.getValue());
        entity.setCompleteTime(SystemUtil.getAllSeconds());

        int row = updateByKey(dbKey, entity);
        DriverReceiveTrip receiveTrip = new DriverReceiveTrip();
        receiveTrip.setTripNo(userTrip.getTripNo());
        final List<DriverReceiveTrip> receiveTripList = driverReceiveTripService.selectList(dbKey, receiveTrip);
        if (row > 0) {
            //删除所有可以接此行程的司机记录
            driverReceiveTripService.delete(dbKey, receiveTrip);

            TripStatusRecord statusRecord = new TripStatusRecord();
            statusRecord.setOrderNo(entity.getTripNo());
            statusRecord.setStatusName(TripStatus.UserCancel.getName());
            statusRecord.setStatusValue(TripStatus.UserCancel.getValue());
            statusRecord.setTip(TripStatus.UserCancel.getTip());
            statusRecord.setTime(SystemUtil.getAllSeconds());

            row = tripStatusRecordService.insert(dbKey, statusRecord);
            //修改用户取消包车业务的次数
            long cancelKcTimes = user.getCancelKcTimes() == null ? 0 : user.getCancelKcTimes();
            User userEntity = new User();
            userEntity.setUserId(user.getUserId());
            userEntity.setCancelKcTimes(cancelKcTimes + 1);
            userService.updateByKey(dbKey, userEntity);

            //如果司机已经接单了，则要修改司机行程里面的乘客行程状态为取消
            if (!StringUtils.isBlank(userTrip.getDriverOpenId())) {
                ServiceTripCrews crewsQuery = new ServiceTripCrews();
                crewsQuery.setDriverOpenId(userTrip.getDriverOpenId());
                crewsQuery.setCrewTripNo(tripNo);
                crewsQuery.setCrewOpenId(openId);

                ServiceTripCrews tripCrews = serviceTripCrewsService.selectOne(dbKey, crewsQuery);
                if (tripCrews != null) {
                    crewsQuery = new ServiceTripCrews();
                    crewsQuery.setTripCrewId(tripCrews.getTripCrewId());
                    crewsQuery.setCanceled(true);
                    serviceTripCrewsService.updateByKey(dbKey, crewsQuery);

                    //司机行程的状态也要同步更新
                    ServiceTrip driverTrip = new ServiceTrip();
                    driverTrip.setTripNo(tripCrews.getDriverTripNo());
                    driverTrip.setStatus(TripStatus.UserCancel.getValue());
                    driverTrip.setCompleteTime(SystemUtil.getAllSeconds());

                    updateByKey(dbKey, driverTrip);
                    //同时添加司机行程状态记录
                    statusRecord = new TripStatusRecord();
                    statusRecord.setOrderNo(tripCrews.getDriverTripNo());
                    statusRecord.setStatusName(TripStatus.UserCancel.getName());
                    statusRecord.setStatusValue(TripStatus.UserCancel.getValue());
                    statusRecord.setTip(TripStatus.UserCancel.getTip());
                    statusRecord.setTime(SystemUtil.getAllSeconds());
                    tripStatusRecordService.insert(dbKey, statusRecord);
                }
            }
            //如果支付成功，且状态为支付完成的状态，需要执行退款功能。
            if (userTrip.getPayFee() != null && userTrip.getPayStatus() != null && userTrip.getPayStatus() == TradePayStatus.TRADE_SUCCESS.getValue()) {
                //判断任务支付方式执行退款功能
                Boolean refundSucceed = false;
                int refundStatus = TripRefundStatus.FAILED.getValue();
                //余额支付
                if (userTrip.getPayId() == PayType.Balance.getId()) {
                    if (user != null) {
                        double beforeBalance = user.getBalance();
                        double afterBalance = user.getBalance() + userTrip.getPayFee();
                        userEntity = new User();
                        userEntity.setUserId(user.getUserId());
                        userEntity.setBalance(afterBalance);
                        int row1 = userService.updateByKey(dbKey, userEntity);
                        refundSucceed = row1 > 0;
                        logger.error("userCancel.Balance.refundSucceed=" + refundSucceed);
                        if (refundSucceed) {
                            //添加用户充值记录
                            RechargeRecord record = new RechargeRecord();
                            record.setRecordNo(SystemUtil.generateRandomNo(""));
                            record.setPhoneNo(user.getOpenId());
                            record.setUserType(PlatformUserType.CLIENT_USER.getValue());
                            record.setRechargeMoney(userTrip.getPayFee());
                            record.setRechargeFrom(RechargeFrom.FROM_SYSTEM.getValue());
                            record.setBeforeBalance(beforeBalance);
                            record.setAfterBalance(afterBalance);
                            record.setRechargeTime(SystemUtil.getAllSeconds());
                            record.setRechargeType(RechargeType.BalanceRefund.getValue());
                            record.setOperator("system");
                            record.setRemark("用户取消包车行程退款，余额退款充值");
                            record.setLinkTaskNo(tripNo);
                            rechargeRecordService.insert(dbKey, record);
                        }
                        refundStatus = refundSucceed ? TripRefundStatus.SUCCEESS.getValue() : TripRefundStatus.FAILED.getValue();
                    }
                }//微信支付
                else if (userTrip.getPayId() == PayType.Tenpay.getId()) {
                    //获取腾讯支付记录
                    String payTradeNo = userTrip.getPayTradeNo();
                    Tenpay query = new Tenpay();
                    query.setTransactionId(payTradeNo);

                    Tenpay tenpay = tenpayService.selectOne(dbKey, query);
                    if (tenpay != null) {
                        WxMpService wxMpService = new WxMpServiceImpl();
                        Double totalFee = userTrip.getPayFee() * 100;
                        int fee = totalFee.intValue();
                        RefundResData refundResData = wxMpService.refund(tenpay.getOutTradeNo(), tenpay.getTransactionId(), "", "", fee, fee, tenpay.getFeeType());

                        refundSucceed = refundResData.isResultSuccess();
                    }
                    refundStatus = refundSucceed ? TripRefundStatus.SUCCEESS.getValue() : TripRefundStatus.FAILED.getValue();

                }//支付宝支付
                else if (userTrip.getPayId() == PayType.Alipay.getId()) {
                    //新的支付宝退款流程：直接调接口退款
                    String payTradeNo = userTrip.getPayTradeNo();
                    Alipay query = new Alipay();
                    query.setTradeNo(payTradeNo);

                    Alipay alipay = alipayService.selectOne(dbKey, query);
                    if (alipay != null) {
                        refundSucceed = AlipayCore.refund(alipay.getOutTradeNo(), alipay.getTradeNo(), alipay.getTotalFee().floatValue(), "user cancel order refund", "", "", "", "");
                        refundStatus = refundSucceed ? TripRefundStatus.SUCCEESS.getValue() : TripRefundStatus.FAILED.getValue();
                        if (refundSucceed) {
                            Alipay refundAlipay = new Alipay();
                            refundAlipay.setPayId(alipay.getPayId());
                            refundAlipay.setRefundFee(alipay.getTotalFee());
                            refundAlipay.setTradeStatus("TRADE_CLOSED");

                            alipayService.updateByKey(dbKey, refundAlipay);
                        }
                    }
                    logger.error("userCancelTask.alipay::refundSucceed" + refundSucceed + ",refundStatus=" + refundStatus);

                }
                TripRefundRecord refundRecord = new TripRefundRecord();
                refundRecord.setTaskNo(tripNo);
                refundRecord.setRefundFee(userTrip.getPayFee());
                refundRecord.setTaskPayId(userTrip.getPayId());
                refundRecord.setTaskPayTradeNo(userTrip.getPayTradeNo());
                refundRecord.setRefundStatus(refundStatus);
                refundRecord.setRefundTime(SystemUtil.getAllSeconds());
                //设置备注为平台资金流动用途
                refundRecord.setRemark(FlowPurpose.USER_CANCEL_TRIP_REFUND.getName());
                //添加任务退款记录
                tripRefundRecordService.insert(dbKey, refundRecord);

                //退款成功后添加平台资金流动记录,支付宝的再后台处理后在添加这个记录
                if (refundSucceed) {
                    PlatformFundFlowRecord flowRecord = new PlatformFundFlowRecord();
                    flowRecord.setRecordNo(SystemUtil.generateRandomNo(""));
                    flowRecord.setUserUniqueId(userTrip.getOpenId());
                    flowRecord.setUserType(PlatformUserType.CLIENT_USER.getValue());
                    flowRecord.setFlowAmount(userTrip.getPayFee());
                    flowRecord.setInOut(FlowInOut.OUT.getValue());
                    flowRecord.setFlowFrom("平台" + userTrip.getPayName());
                    flowRecord.setFlowTo("用户" + userTrip.getPayName());
                    flowRecord.setFlowMode(userTrip.getPayId());
                    flowRecord.setLinkNo(tripNo);
                    flowRecord.setPurpose(FlowPurpose.USER_CANCEL_TRIP_REFUND.getName());
                    flowRecord.setRemark("用户支付行程费用退还给用户");

                    platformFundFlowRecordService.insert(dbKey, flowRecord);
                }

            }
        }
        //取消成功，则推送
        if (row > 0) {
            if (receiveTripList != null & receiveTripList.size() > 0) {
                //推送给用户取消行程成功
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String toUser = openId;
                        List<String> aliasList = new ArrayList<String>();
                        for (DriverReceiveTrip driverReceiveTrip : receiveTripList) {
                            aliasList.add(driverReceiveTrip.getDriverOpenId());
                            toUser = driverReceiveTrip.getDriverOpenId();
                        }
                        String message = String.format("用户取消%s行程", CustomMsgModule.KC.getName());
                        String content = String.format("你已成功取消[%s]行程", CustomMsgModule.KC.getName());
                        PushCustomMsg customMsg = new PushCustomMsg();
                        customMsg.setModule(CustomMsgModule.KC.getValue());
                        customMsg.setType(CustomMsgType.UserCancelTrip.getValue());
                        customMsg.setKey(tripNo);

                        PushMessage pushMessage = new PushMessage();
                        pushMessage.setContent(content);
                        pushMessage.setModule(CustomMsgModule.KC.getValue());
                        pushMessage.setTitle(message);
                        pushMessage.setTripNo(tripNo);
                        pushMessage.setToUser(toUser);
                        pushMessage.setMessage(message);
                        pushMessage.setContent(content);
                        pushMessage.setExtras(customMsg.toJson());
                        pushMessage.setPushTime(SystemUtil.getAllSeconds());
                        pushMessage.setJumpUrl("");
                        pushMessageService.pushToUser(dbKey, pushMessage, PushTargetApp.Driver, customMsg, aliasList, true);
                    }
                }).start();
            }
            //司机接单或用户已经上车的状态都要推送给司机消息
            if (userTrip.getStatus() >= TripStatus.Accept.getValue() &&
                    userTrip.getStatus() < TripStatus.Complete.getValue()) {
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        List<String> aliasList = new ArrayList<String>();
                        aliasList.add(userTrip.getDriverOpenId());
                        String toUser = userTrip.getDriverOpenId();
                        String message = String.format("用户取消%s行程", CustomMsgModule.KC.getName());
                        String content = String.format("用户已成功取消[%s]行程,行程单号为：%s", CustomMsgModule.KC.getName(), tripNo);
                        PushCustomMsg customMsg = new PushCustomMsg();
                        customMsg.setModule(CustomMsgModule.KC.getValue());
                        customMsg.setType(CustomMsgType.UserCancelTrip.getValue());
                        customMsg.setKey(tripNo);

                        PushMessage pushMessage = new PushMessage();
                        pushMessage.setContent(content);
                        pushMessage.setModule(CustomMsgModule.KC.getValue());
                        pushMessage.setTitle(message);
                        pushMessage.setTripNo(tripNo);
                        pushMessage.setToUser(toUser);
                        pushMessage.setMessage(message);
                        pushMessage.setContent(content);
                        pushMessage.setExtras(customMsg.toJson());
                        pushMessage.setPushTime(SystemUtil.getAllSeconds());
                        pushMessage.setJumpUrl("");

                        pushMessageService.pushToUser(dbKey, pushMessage, PushTargetApp.Driver, customMsg, aliasList, true);
                    }
                }).start();
            }
        }
        if (row > 0) {
            try {
                OrderCancel orderCancel = new OrderCancel();
                orderCancel.setCompanyId("5201HDCX8779");
                orderCancel.setAddress(520100L);
                orderCancel.setOrderId(userTrip.getTripNo());
                Date start = new Date(userTrip.getCreateTime() * 1000L);
                orderCancel.setOrderTime(Long.parseLong(DateUtil.DatetoString(start, "yyyyMMddHHmmss")));
                orderCancel.setCancelTime(Long.parseLong(DateUtil.DatetoString(new Date(), "yyyyMMddHHmmss")));
                orderCancel.setOperator("1");
                orderCancel.setCancelTypeCode("1");
                orderCancel.setCancelReason("");
                orderCancel.setCancelTime2(0L);
                orderCancelService.insertUpper("", orderCancel);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return row > 0;
    }


    @Override
    @Transactional
    public synchronized Boolean driverAccept(final String dbKey, final ServiceTrip userTrip, final ServiceTrip driverTrip) throws Exception {
        if (userTrip == null)
            return false;
        String driverTripNo = driverTrip.getTripNo();
        ServiceTrip userTripEntity = new ServiceTrip();
        //此参数在外部设置，这里取出来修改
        userTripEntity.setDriverOpenId(userTrip.getDriverOpenId());
        userTripEntity.setTripNo(userTrip.getTripNo());
        userTripEntity.setStatus(TripStatus.Accept.getValue());
        int row = updateByKey(dbKey, userTripEntity);
        if (row > 0) {
            //添加行程状态
            TripStatusRecord statusRecord = new TripStatusRecord();
            statusRecord.setOrderNo(userTrip.getTripNo());
            statusRecord.setStatusName(TripStatus.Accept.getName());
            statusRecord.setStatusValue(TripStatus.Accept.getValue());
            statusRecord.setTip(TripStatus.Accept.getTip());
            statusRecord.setTime(SystemUtil.getAllSeconds());
            row = tripStatusRecordService.insert(dbKey, statusRecord);
            //删除此行程的可接单记录
            DriverReceiveTrip receiveTrip = new DriverReceiveTrip();
            receiveTrip.setTripNo(userTrip.getTripNo());
            driverReceiveTripService.delete(dbKey, receiveTrip);
        }
        if (row > 0) {
            //系统创建司机行程，与用户的行程一样，为了与专线和顺风车业务统一
            driverTrip.setServiceCode(userTrip.getServiceCode());
            driverTrip.setUserType(PlatformUserType.DRIVER_USER.getValue());
            driverTrip.setOpenId(userTrip.getOpenId());
            driverTrip.setDriverOpenId(userTrip.getDriverOpenId());
            driverTrip.setRoadId(userTrip.getRoadId());
            driverTrip.setGoTime(userTrip.getGoTime());
            driverTrip.setStatus(TripStatus.Accept.getValue());
            driverTrip.setTripType(userTrip.getTripType());
            driverTrip.setCrewCount(userTrip.getCrewCount());
            //费用
            driverTrip.setPayFee(userTrip.getPayFee());
            driverTrip.setdCommonId(userTrip.getdCommonId());
            driverTrip.setDepartTime(userTrip.getDepartTime());
            //其他默认值
            driverTrip.setHasAsign(false);
            driverTrip.setHasComment(false);
            driverTrip.setHasComplaint(false);
            //添加司机行程
            updateByKey(dbKey, driverTrip);
            //添加行程状态
            TripStatusRecord statusRecord = new TripStatusRecord();
            statusRecord.setOrderNo(userTrip.getTripNo());
            statusRecord.setStatusName(TripStatus.Accept.getName());
            statusRecord.setStatusValue(TripStatus.Accept.getValue());
            statusRecord.setTip(TripStatus.Accept.getTip());
            statusRecord.setTime(SystemUtil.getAllSeconds());
            row = tripStatusRecordService.insert(dbKey, statusRecord);

            //添加司机行程人数记录
            ServiceTripCrews crewsEntity = new ServiceTripCrews();
            crewsEntity.setDriverTripNo(driverTripNo);//司机与用户为同一个行程
            crewsEntity.setCrewTripNo(userTrip.getTripNo());
            crewsEntity.setDriverOpenId(userTrip.getDriverOpenId());
            crewsEntity.setCrewOpenId(userTrip.getOpenId());
            crewsEntity.setCrewStatus(TripCrewStatus.OffTheCar.getValue());
            Boolean payStatus = userTrip.getPayStatus() != null &&
                    (userTrip.getPayStatus() == TradePayStatus.TRADE_SUCCESS.getValue()
                            || userTrip.getPayStatus() == TradePayStatus.TRADE_FINISHED.getValue());
            crewsEntity.setPayStatus(payStatus);
            crewsEntity.setCanceled(false);

            row = serviceTripCrewsService.insert(dbKey, crewsEntity);
        }
        //推送给用户告知司机已接单
        if (row > 0) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //用户行程单号
                    String tripNo = userTrip.getTripNo();
                    List<String> aliasList = new ArrayList<String>();
                    aliasList.add(userTrip.getOpenId());
                    String toUser = userTrip.getOpenId();
                    String message = String.format("司机接受%s行程", CustomMsgModule.ZXPC.getName());
                    String content = String.format("司机已成功接受[%s]行程,行程单号为：%s", CustomMsgModule.ZXPC.getName(), tripNo);
                    PushCustomMsg customMsg = new PushCustomMsg();
                    customMsg.setModule(CustomMsgModule.ZXPC.getValue());
                    customMsg.setType(CustomMsgType.DriverAcceptTrip.getValue());
                    customMsg.setKey(tripNo);

                    PushMessage pushMessage = new PushMessage();
                    pushMessage.setContent(content);
                    pushMessage.setModule(CustomMsgModule.KC.getValue());
                    pushMessage.setTitle(message);
                    pushMessage.setTripNo(tripNo);
                    pushMessage.setToUser(toUser);
                    pushMessage.setMessage(message);
                    pushMessage.setContent(content);
                    pushMessage.setExtras(customMsg.toJson());
                    pushMessage.setPushTime(SystemUtil.getAllSeconds());
                    pushMessage.setJumpUrl("");

                    pushMessageService.pushToUser(dbKey, pushMessage, PushTargetApp.Client, customMsg, aliasList, true);
                }
            }).start();
        }

        try {
            Driver driver = driverService.getDriverByOpenId("", userTrip.getDriverOpenId());
            if (driver != null) {
                OrderMatch orderMatch = new OrderMatch();
                orderMatch.setCompanyId("5201HDCX8779");
                orderMatch.setAddress(520100L);
                orderMatch.setOrderId(userTrip.getTripNo());

                orderMatch.setLongitude(driver.getLon());
                orderMatch.setLatitude(driver.getLat());
                orderMatch.setEncrypt(3L);
                orderMatch.setLicenseId(driver.getIdCard());
                orderMatch.setDriverPhone(driver.getPhoneNo());
                orderMatch.setVehicleNo(driver.getCarNo());
                orderMatch.setDistributeTime(Long.parseLong(DateUtil.DatetoString(new Date(), "yyyyMMddHHmmss")));
                orderMatch.setDistributeTime2(0L);
                orderMatchService.insertUpper("", orderMatch);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

}
